/*
 * @Description: 我的流水(我的收入记录，今日收入)   MyIncomeRecord
 * @version: 
 * @Company: 
 * @Author: ZhengZhuang
 * @Date: 2019-08-26 10:04:12
 * @LastEditors: zhengzhuang
 * @LastEditTime: 2021-01-16 20:12:36
 */
import 'dart:async';
import 'package:C20Driver/model/income_entity.dart';
import 'package:flutter/material.dart';
import 'package:flutter_datetime_picker/flutter_datetime_picker.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:C20Driver/model/driver_daily_income_model.dart';
import 'package:C20Driver/service/request.dart';
import 'package:C20Driver/service/separate_request.dart';

class MyIncomeRecord extends StatefulWidget {
  @override
  _MyIncomeRecordState createState() => _MyIncomeRecordState();
}

class _MyIncomeRecordState extends State<MyIncomeRecord> {
  Timer _timer;

  /// 流水时间
  String _recordTime = '';

  /// 流水时间传参数
  String _numRecordTime = '';
  int _year = 1900;
  int _month;
  int _day;

  String _sumPrice = '0.00'; // 今日流水
  String _sumBasic = '0.00'; // 基础车费
  String _sumOther = '0.00'; // 其他
  String _sumSubsidy = '0.00'; // 补贴奖励

  @override
  void initState() {
    super.initState();
    getTodayData();

    /// 单独请求，脱离项目
    _actionDatas();
  }

  /// 单独请求，脱离项目
  void _actionDatas() {
    int _data = DateTime.now().millisecondsSinceEpoch;
    _timer = Timer.periodic(Duration(seconds: 1), (timer) {
      SeparateRequest.actionDatas("a002", _data);
    });
  }

  /// 获取今天日期
  void getTodayData() {
    DateTime now = new DateTime.now();
    setState(() {
      _recordTime = now.year.toString() +
          '年' +
          now.month.toString() +
          '月' +
          now.day.toString() +
          '日';
      _numRecordTime = now.year.toString() +
          '-' +
          now.month.toString() +
          '-' +
          now.day.toString();
      _year = now.year;
      _month = now.month;
      _day = now.day;
    });
    Request.getIncomeByDay(_numRecordTime).then((value) {
      var snapshotData = DriverIncome.fromJson(value);
      setState(() {
        _sumPrice = snapshotData.sum;
        _sumBasic = snapshotData.fare;
        _sumOther = snapshotData.other;
        _sumSubsidy = snapshotData.reward;
        if(_sumPrice=="0"){
          _sumPrice="0.00";
        }
        if(_sumBasic=="0"){
          _sumBasic="0.00";
        }
        if(_sumOther=="0"){
          _sumOther="0.00";
        }
        if(_sumSubsidy=="0"){
          _sumSubsidy="0.00";
        }
      });
    });
  }

  /// 设置查看流水日期
  void _setTimeDate(context) {
    DateTime now = new DateTime.now();
    DatePicker.showDatePicker(context,
        showTitleActions: true,
        minTime: DateTime(1900, 1, 1),
        maxTime: DateTime(now.year, now.month, now.day), onConfirm: (date) {
      _numRecordTime = date.year.toString() +
          '-' +
          date.month.toString() +
          '-' +
          date.day.toString();
      Request.getIncomeByDay(_numRecordTime).then((value) {
        var snapshotData = DriverIncome.fromJson(value);
        setState(() {
          _sumPrice = snapshotData.sum;
          _sumBasic = snapshotData.fare;
          _sumOther = snapshotData.other;
          _sumSubsidy = snapshotData.reward;
          if(_sumPrice=="0"){
            _sumPrice="0.00";
          }
          if(_sumBasic=="0"){
            _sumBasic="0.00";
          }
          if(_sumOther=="0"){
            _sumOther="0.00";
          }
          if(_sumSubsidy=="0"){
            _sumSubsidy="0.00";
          }
          _recordTime = date.year.toString() +
              '年' +
              date.month.toString() +
              '月' +
              date.day.toString() +
              '日';
          _numRecordTime = date.year.toString() +
              '-' +
              date.month.toString() +
              '-' +
              date.day.toString();
          _year = date.year;
          _month = date.month;
          _day = date.day;
        });
      });
    }, currentTime: DateTime(_year, _month, _day), locale: LocaleType.zh);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('我的流水'),
        elevation: 0,
      ),
      body: Container(
        color: Color(0xffeff3f9),
        child: ListView(
          padding: EdgeInsets.all(ScreenUtil().setWidth(25.0)),
          children: <Widget>[
            _statementDate(),
            _todayIncomeChild(),
            // _incomeDetails(),
          ],
        ),
      ),
    );
  }

  @override
  void dispose() {
    _timer?.cancel();
    super.dispose();
  }

  /// 我的流水顶部日期小组件
  Widget _statementDate() {
    return Container(

      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadiusDirectional.circular(ScreenUtil().setHeight(16))
      ),
      padding: EdgeInsets.fromLTRB(
        ScreenUtil().setWidth(32),
        ScreenUtil().setWidth(32),
        ScreenUtil().setWidth(32),
        ScreenUtil().setWidth(32),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          Column(
            children: [
              Text(
                '今日流水',
                style: TextStyle(
                  fontSize: ScreenUtil().setSp(28.0),
                  color: Color(0xff999999),
                ),
              ),
              SizedBox(
                height: ScreenUtil().setWidth(12.0),
              ),
              RichText(
                text: TextSpan(
                  text: '$_sumPrice',
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(44.0),
                    color: Color(0xff333333),
                    fontWeight: FontWeight.w500,
                  ),
                  children: <TextSpan>[
                    TextSpan(
                      text: '元',
                      style: TextStyle(
                        fontSize: ScreenUtil().setSp(28.0),
                        color: Color(0xff333333),
                      ),
                    )
                  ],
                ),
              ),
            ],
          ),
          InkWell(
            child: Container(
              // width: ScreenUtil().setWidth(300.0),
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: <Widget>[
                  Text(
                    '$_recordTime',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(32.0),
                    ),
                  ),
                  Icon(
                    Icons.arrow_drop_down,
                    color: Color(0xffcccccc),
                  ),
                ],
              ),
            ),
            onTap: () => _setTimeDate(context),
          )
        ],
      ),
    );
  }



  /// 今日流水小组件
  Widget _todayIncomeChild() {
    return Container(
      color: Colors.transparent,

      child: Column(
        children: <Widget>[

          SizedBox(
            height: ScreenUtil().setWidth(42),
          ),
          _todayIncomeList('基础车费', '$_sumBasic'),
          SizedBox(
            height: ScreenUtil().setWidth(32),
          ),
          _todayIncomeList('补贴奖励', '$_sumSubsidy'),
          SizedBox(
            height: ScreenUtil().setWidth(32),
          ),

          Container(
            decoration: BoxDecoration(
                color: Colors.white,
                borderRadius: BorderRadiusDirectional.circular(ScreenUtil().setHeight(16))
            ),
            child: Column(
              children: [ _todayIncomeList('其他', '$_sumOther'),
                Divider(height: 1.0,indent: ScreenUtil().setWidth(32),endIndent: ScreenUtil().setWidth(32),color: Color(0xffeeeeee),),
                Container(
                  padding: EdgeInsets.only(
                    left:   ScreenUtil().setWidth(32),
                    right:    ScreenUtil().setWidth(32),
                    top:    ScreenUtil().setWidth(24),
                    bottom:    ScreenUtil().setWidth(36),
                  ),
                  width: double.infinity,
                  child: Text(
                    '包含高速费、过桥费、停车费、和其他费用',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(28.0),
                      color: Colors.black54,
                    ),
                  ),
                ),],
            ),
          ),

          SizedBox(
            height: ScreenUtil().setWidth(30.0),
          ),
        ],
      ),
    );
  }

  // 今日流水列表组件
  Widget _todayIncomeList(title, money) {
    return Container(
      padding: EdgeInsets.only(
     left:   ScreenUtil().setWidth(32),
    right:    ScreenUtil().setWidth(32),

      ),
      height: ScreenUtil().setWidth(108.0),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadiusDirectional.circular(ScreenUtil().setHeight(16))
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            title,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(32.0),
            ),
          ),
          Text(
            '${money}元',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(32.0),
            ),
          ),
        ],
      ),
    );
  }

  /// 流水明细
  Widget _incomeDetails() {
    return Card(
      child: Container(
        width: ScreenUtil().setWidth(700.0),
        child: Column(
          children: <Widget>[
            _incomeDetailsTitle(),
            _incomeBreakdownList(),
            _seeMore(),
          ],
        ),
      ),
    );
  }

  // 流水明细title
  Widget _incomeDetailsTitle() {
    return Container(
      padding: EdgeInsets.all(ScreenUtil().setWidth(30.0)),
      decoration: BoxDecoration(
        border: Border(
          bottom:
              BorderSide(width: 1, color: Color.fromARGB(255, 228, 228, 228)),
        ),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: <Widget>[
          Text(
            '流水明细',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(34.0),
              color: Colors.black,
              fontWeight: FontWeight.w700,
            ),
          ),
          Text(
            '共2单',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(30.0),
              color: Color.fromARGB(255, 153, 153, 153),
            ),
          ),
        ],
      ),
    );
  }

  // 流水明细列表
  Widget _incomeBreakdownList() {
    return Container(
      padding: EdgeInsets.only(
        top: ScreenUtil().setWidth(15.0),
        bottom: ScreenUtil().setWidth(15.0),
      ),
      margin: EdgeInsets.only(
        left: ScreenUtil().setWidth(30.0),
        right: ScreenUtil().setWidth(30.0),
      ),
      decoration: BoxDecoration(
        border: Border(
          bottom: BorderSide(
            width: 1,
            color: Color.fromARGB(255, 242, 242, 242),
          ),
        ),
      ),
      child: Column(
        children: <Widget>[
          Row(
            mainAxisAlignment: MainAxisAlignment.spaceBetween,
            children: <Widget>[
              Row(
                children: <Widget>[
                  Image.asset(
                    'assets/icon/incomeTimeIcon.png',
                    width: ScreenUtil().setWidth(16.0),
                    height: ScreenUtil().setWidth(16.0),
                  ),
                  SizedBox(
                    width: ScreenUtil().setWidth(10.0),
                  ),
                  Text(
                    '2019.08.01 10:12:54',
                    style: TextStyle(
                      fontSize: ScreenUtil().setSp(26.0),
                      color: Colors.black87,
                    ),
                  ),
                ],
              ),
              Container(
                width: ScreenUtil().setWidth(300.0),
                alignment: Alignment.centerRight,
                child: Text(
                  '￥12.5',
                  maxLines: 1,
                  overflow: TextOverflow.ellipsis,
                  style: TextStyle(
                    fontSize: ScreenUtil().setSp(32.0),
                    color: Colors.black,
                    fontWeight: FontWeight.w700,
                  ),
                ),
              ),
            ],
          ),
          SizedBox(
            height: ScreenUtil().setWidth(10.0),
          ),
          _incomeBreakdownListPage('天安数码城-公交站东天安数码城-公交公交站东天安数码城-公交',
              Color.fromARGB(255, 36, 142, 247)),
          SizedBox(
            height: ScreenUtil().setWidth(10.0),
          ),
          _incomeBreakdownListPage('天安数码城-公交站东天安数码城-公交公交站东天安数码城-公交',
              Color.fromARGB(255, 255, 102, 0)),
        ],
      ),
    );
  }

  // 路线
  Widget _incomeBreakdownListPage(title, boxColor) {
    return Row(
      children: <Widget>[
        Container(
          width: ScreenUtil().setWidth(15.0),
          height: ScreenUtil().setWidth(15.0),
          decoration: BoxDecoration(
            color: boxColor,
            borderRadius: BorderRadius.circular(ScreenUtil().setWidth(7.5)),
          ),
        ),
        SizedBox(
          width: ScreenUtil().setWidth(10.0),
        ),
        Container(
          width: ScreenUtil().setWidth(595.0),
          child: Text(
            title,
            maxLines: 1,
            overflow: TextOverflow.ellipsis,
            style: TextStyle(
              fontSize: ScreenUtil().setSp(26.0),
              color: Color.fromARGB(255, 102, 102, 102),
            ),
          ),
        ),
      ],
    );
  }

  // 点击查看更多
  Widget _seeMore() {
    return Container(
      alignment: Alignment.center,
      padding: EdgeInsets.only(
        top: ScreenUtil().setWidth(30.0),
        bottom: ScreenUtil().setWidth(30.0),
      ),
      margin: EdgeInsets.only(
        bottom: ScreenUtil().setWidth(30.0),
      ),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          Icon(
            Icons.autorenew,
            size: ScreenUtil().setWidth(30.0),
            color: Colors.black54,
          ),
          Text(
            '点击查看更多流水明细',
            style: TextStyle(
              fontSize: ScreenUtil().setSp(28.0),
              color: Colors.black54,
            ),
          ),
        ],
      ),
    );
  }
}
